package com.hmall.item.web;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.constants.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;



    //分页查询
    @GetMapping("/list")
    public PageDTO<Item> selectPage(@RequestParam Integer page, @RequestParam Integer size){
        //构造条件构造器
        Page<Item> pageinfo = new Page<>(page, size);
//        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.orderByAsc(Item::getCreateTime);
        itemService.page(pageinfo);
        return new PageDTO<>(pageinfo.getTotal(),pageinfo.getRecords());
    }

    //根据id查询
    @GetMapping("/{id}")
    public Item selectById(@PathVariable Long id){
        //构造条件构造器
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getId,id);
        Item item = itemService.getOne(queryWrapper);
        return item;
    }

    //新增商品
    @PostMapping
    public void save(@RequestBody Item item){
        Date date = new Date(System.currentTimeMillis());
        item.setCreateTime(date);
        item.setUpdateTime(date);
        itemService.save(item);
        //rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_INSERT_KEY,item.getId());
    }

    //商品上架下架
    @PutMapping("/status/{id}/{status}")
    public void update(@PathVariable Long id,@PathVariable Integer status){
        //构造条件构造器
        LambdaUpdateWrapper<Item> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Item::getId,id);
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemService.update(item,updateWrapper);
        if(status==1){
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_INSERT_KEY,id);
        }else {
            rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DELETE_KEY,id);
        }
    }

    //修改商品信息
    @PutMapping
    public void updateById(@RequestBody Item item){
        Date date = new Date(System.currentTimeMillis());
        item.setUpdateTime(date);
        itemService.updateById(item);
        //rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_INSERT_KEY,item.getId());
    }

    //根据id删除
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id){
        itemService.removeById(id);
        //rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE,MqConstants.ITEM_DELETE_KEY,id);
    }


}
